package com.simple.action.slr;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.EmpEmployee;
import com.simple.po.SlrKeepGrant;
import com.simple.service.slr.SlrKeepGrantService;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.core.SessionBean;
import com.xunj.po.SysCodeDict;
import com.xunj.util.Common;

/**
 * 费用报销
 * @author 王瑾
 *
 */
public class SlrKeepGrantAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -365972686059686745L;
	private SlrKeepGrant slrKeepGrant; //工资留存


	



	/**
	 * 查询核销完毕的数据
	 * @return
	 * @throws Exception
	 */
	public String listSlrKeepGrant() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		paralist.add(new ParaUnit("state", "在用", ParaUnit.EQ));
		
		if(POST())
		{
			if(!slrKeepGrant.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", slrKeepGrant.getAreaId(), ParaUnit.EQ));
			if(!slrKeepGrant.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", slrKeepGrant.getAreaDeptId(), ParaUnit.EQ));
			String feeDateFrom = request.getParameter("feeDateFrom");
			String feeDateTo = request.getParameter("feeDateTo");
			if(!feeDateFrom.equals(""))
			{
				paralist.add(new ParaUnit("slrKeepGrantDate",Common.newDate(feeDateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
				request.setAttribute("feeDateFrom", feeDateFrom);
			}
			if(!feeDateTo.equals(""))
			{
				paralist.add(new ParaUnit("slrKeepGrantDate",Common.newDate(feeDateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
				request.setAttribute("feeDateTo", feeDateTo);
			}
			if(!slrKeepGrant.getKeepGrantType().equals(""))
				paralist.add(new ParaUnit("keepGrantType", slrKeepGrant.getKeepGrantType(), ParaUnit.EQ));			
			if(!slrKeepGrant.getTargetName().equals(""))
				paralist.add(new ParaUnit("slrKeepGrantCompanyName", slrKeepGrant.getTargetName(), ParaUnit.LIKE));			
		}
		List list = dao.criteriaByPage(SlrKeepGrant.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/slr/salary_keep_grant/lst_salary_keep_grant.jsp");
	}
	/**
	 * 判断预留金额是否大于发放金额
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkAmountSlrKeepGrant() throws Exception {
		
		SlrKeepGrantService fgs = new SlrKeepGrantService(dao);
		String data = fgs.checkSlrKeepGrantAmount(slrKeepGrant);
		
		request.setAttribute("data", data);
		return toResult();

	}
	/**
	 * 添加工资留存发放
	 * @return
	 * @throws Exception
	 */
	public String addSlrKeepGrant() throws Exception
	{
		if(POST())
		{
			String submitType = request.getParameter("submitType");
			HttpServletRequest request = ServletActionContext.getRequest();
			SessionBean sessionBean  = SessionBean.getSessionBean(request);
			slrKeepGrant.setSalaryKeepGrantId(UUID.randomUUID().toString());
			slrKeepGrant.setState("在用");
			slrKeepGrant.setCreateUserId(sessionBean.getUserId());
			slrKeepGrant.setCreateUserName(sessionBean.getUserName());
			slrKeepGrant.setCreateDate(new Date());
			
			dao.save(slrKeepGrant);
			if("save".equals(submitType))
			{
				this.Alert_Close("保存成功", "ok");
				return toMessage();
			}
			else
			{
				if(slrKeepGrant.getTargetId()!=null&&!"".equals(slrKeepGrant.getTargetId()))
					request.setAttribute("grantTargetType", "人员");
				else
					request.setAttribute("grantTargetType", "非人员");
				return toJsp("/jsp/slr/salary_keep_grant/add_salary_keep_grant.jsp");
			}
			
		}
		else
		{
			return toJsp("/jsp/slr/salary_keep_grant/add_salary_keep_grant.jsp");
		}
	}
	/**
	 * 动态显示费用对象数据
	 * @return
	 * @throws Exception
	 */
	public String showTargetSlrKeepGrant() throws Exception
	{
		String feeTarget = request.getParameter("feeTarget");
		String data = "<option value=\"\">请选择</option>";

		String areaId = request.getParameter("areaId");
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		
		paralist.add(new ParaUnit("state", "在用", ParaUnit.EQ));
		paralist.add(new ParaUnit("employeeOrder", ParaUnit.ASC,ParaUnit.ORDER));
		paralist.add(new ParaUnit("areaId", areaId, ParaUnit.EQ));
		List list = dao.criteriaQuery(EmpEmployee.class, paralist);
		
		for(int i=0;i<list.size();i++)
		{
			EmpEmployee emp = (EmpEmployee) list.get(i);
			data+="<option value=\""+emp.getEmployeeId()+"\">"+emp.getEmployeeName()+"</option>";
		}
		request.setAttribute("data", data);
		return toResult();
	}
	/**
	 * 修改工资留存
	 * @return
	 * @throws Exception
	 */
	public String updateSlrKeepGrant() throws Exception
	{
		if(POST())
		{
			String submitType = request.getParameter("submitType");
			dao.update(slrKeepGrant);
			if("save".equals(submitType))
			{
				this.Alert_Close("保存成功", "ok");
				return toMessage();
			}
			else
			{
				return toJsp("/jsp/slr/salary_keep_grant/add_salary_keep_grant.jsp");
			}
		}
		else
		{
			String salaryKeepGrantId = request.getParameter("salaryKeepGrantId");
			slrKeepGrant = (SlrKeepGrant)dao.findOne(SlrKeepGrant.class, salaryKeepGrantId);
			if(slrKeepGrant.getTargetId()!=null&&!"".equals(slrKeepGrant.getTargetId()))
				request.setAttribute("grantTargetType", "人员");
			else
				request.setAttribute("grantTargetType", "非人员");
			return toJsp("/jsp/slr/salary_keep_grant/update_salary_keep_grant.jsp");
		}
	}
	/**
	 * 删除工资留存
	 * @return
	 * @throws Exception
	 */
	public String deleteSlrKeepGrant() throws Exception
	{
		String salaryKeepGrantId = request.getParameter("salaryKeepGrantId");
		slrKeepGrant = (SlrKeepGrant)dao.findOne(SlrKeepGrant.class, salaryKeepGrantId);
		String data = "ok";
		if(slrKeepGrant.getKeepGrantType().equals("留存"))
		{
			SlrKeepGrantService fgs = new SlrKeepGrantService(dao);
			data = fgs.checkSlrKeepGrantAmount(slrKeepGrant);
		}
		if(data.equals("ok"))
		{
			slrKeepGrant.setState("停用");
			dao.update(slrKeepGrant);
			this.Alert_Close("删除成功", "ok");
		}else{
			this.Alert_Close("留存费用小于发放费用，数据无法被删除！", "ok");
		}
		return toMessage();

	}
	/**
	 * 查看工资留存
	 * @return
	 * @throws Exception
	 */
	public String showSlrKeepGrant() throws Exception
	{
		String salaryKeepGrantId = request.getParameter("salaryKeepGrantId");
		slrKeepGrant = (SlrKeepGrant)dao.findOne(SlrKeepGrant.class, salaryKeepGrantId);
		if(slrKeepGrant.getTargetId()!=null&&!"".equals(slrKeepGrant.getTargetId()))
			request.setAttribute("grantTargetType", "人员");
		else
			request.setAttribute("grantTargetType", "非人员");
		return toJsp("/jsp/slr/salary_keep_grant/show_salary_keep_grant.jsp");
	}
	public SlrKeepGrant getSlrKeepGrant() {
		return slrKeepGrant;
	}

	public void setSlrKeepGrant(SlrKeepGrant slrKeepGrant) {
		this.slrKeepGrant = slrKeepGrant;
	}

}